//
//  GWStatusePhotosView.m
//  新浪微博
//
//  Created by qianfeng on 16/2/19.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWStatusePhotosView.h"
#import "GWStatusePhotoView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
//#import "UIImageView+WebCache.h"
#import "GWStatuseModel.h"
#define statusePhotoW 70
#define statusePhotoH statusePhotoW
#define statusePhotoMargin 10
#define statusePhotoMaxCount 9
#define statusePhotoMaxCols(count) (count)==4?2:3

@interface GWStatusePhotosView()

@property (nonatomic,assign)CGRect lastFrame;
@property (nonatomic,weak)UIImageView *imageView;
@end
@implementation GWStatusePhotosView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        for (int i=0;i<statusePhotoMaxCount ; i++) {
            GWStatusePhotoView *photoView =[[GWStatusePhotoView alloc]init];
            
            UITapGestureRecognizer *tgr =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photoOnTouch:)];
            [photoView addGestureRecognizer:tgr];
            photoView.tag =i;
            [self addSubview:photoView];
        }
    }
    return self;
}
-(void)photoOnTouch:(UITapGestureRecognizer *)tgr
{
    //1.创建图片浏览器
    MJPhotoBrowser *photoBrowser =[[MJPhotoBrowser alloc]init];
    
    //2.设置图片浏览器显示的所有图片
    NSMutableArray *mjPhotoA =[NSMutableArray array];
    NSInteger count =self.pic_urls.count;
    for (int i=0; i<count; i++) {
        GWPhotoModel *photo =self.pic_urls[i];
        MJPhoto *mjPhoto =[[MJPhoto alloc]init];
        //设置图片的路径
        mjPhoto.url =[NSURL URLWithString:photo.bmiddle_pic];
        //设置图片来源于哪个UIImageview
        mjPhoto.srcImageView =self.subviews[i];
        [mjPhotoA addObject:mjPhoto];
    }
    photoBrowser.photos =mjPhotoA;
    photoBrowser.currentPhotoIndex =tgr.view.tag;
    
    //3.显示浏览器
    [photoBrowser show];
}
-(void)setPic_urls:(NSArray *)pic_urls
{
    _pic_urls =pic_urls;
    for (int i=0; i<statusePhotoMaxCount; i++) {
        GWStatusePhotoView *imageView =self.subviews[i];
        if (i<pic_urls.count) {
            imageView.hidden =NO;
            imageView.photoModel =pic_urls[i];
        }
        else
        {
            imageView.hidden =YES;
        }
    }
    
}
-(void)layoutSubviews
{
    int maxCols =statusePhotoMaxCols(self.pic_urls.count);
    for (int i=0; i<statusePhotoMaxCount; i++)
    {
        GWStatusePhotoView *imageView =self.subviews[i];
        int col =i%maxCols;
        int row =i/maxCols;
        CGFloat imageViewX =col*(statusePhotoW+statusePhotoMargin);
        CGFloat imageViewY =row*(statusePhotoH+statusePhotoMargin);
        imageView.frame =CGRectMake(imageViewX, imageViewY, statusePhotoW, statusePhotoH);
    }
}

+(CGSize)photosViewSizeWithPhotoCount:(int)count
{
    int maxCols =statusePhotoMaxCols(count);
    int totolCols =count>maxCols?maxCols:count;
    int totolRows =(count +maxCols-1)/maxCols;
    
    CGFloat photosW =statusePhotoW * totolCols +(totolCols-1)*statusePhotoMargin;
    CGFloat photosH =statusePhotoH * totolRows +(totolRows-1)*statusePhotoMargin;
    return CGSizeMake(photosW, photosH);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


/*
 GWStatusePhotoView *photoView =(GWStatusePhotoView *)tgr.view;
 UIView *mask =[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
 mask.backgroundColor =[UIColor blackColor];
 [[UIApplication sharedApplication].keyWindow addSubview:mask];
 
 UIImageView *imageView =[[UIImageView alloc]init];
 GWPhotoModel *photo =self.pic_urls[tgr.view.tag-1];
 [imageView sd_setImageWithURL:[NSURL URLWithString:photo.bmiddle_pic] placeholderImage:photoView.image];
 imageView.frame =[self convertRect:photoView.frame toView:mask];
 [mask addSubview:imageView];
 self.lastFrame =imageView.frame;
 self.imageView =imageView;
 [UIView animateWithDuration:0.3 animations:^{
 
 CGFloat imageW =mask.width;
 CGFloat imageH =mask.width *(imageView.width/imageView.height);
 imageView.frame =CGRectMake(0, (mask.height-imageH)/2, imageW, imageH);
 }];
 
 UITapGestureRecognizer *tgrM =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(maskOnTouch:)];
 [mask addGestureRecognizer:tgrM];
 }
 -(void)maskOnTouch:(UITapGestureRecognizer *)tgrM
 {
 UIView *mask =tgrM.view;
 [UIView animateWithDuration:0.3 animations:^{
 self.imageView.frame =self.lastFrame;
 mask.backgroundColor =[UIColor clearColor];
 }completion:^(BOOL finished) {
 
 [mask removeFromSuperview];
 }];
 
 }
 */
@end
